import { asyncRouterMap, constantRouterMap } from '@/config/router.config'
import { generatorDynamicRouter } from '@/router/generator-routers'
import {
  RouteView,
  PageView
} from '@/layouts'

/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
// function hasPermission (permission, route) {
//   if (route.meta && route.meta.permission) {
//     let flag = false
//     for (let i = 0, len = permission.length; i < len; i++) {
//       flag = route.meta.permission.includes(permission[i])
//       if (flag) {
//         return true
//       }
//     }
//     return false
//   }
//   return true
// }

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole (roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(roles.id)
  } else {
    return true
  }
}

function filterAsyncRouter (routerMap, roles) {
  const accessedRouters = routerMap.filter(route => {
    return true
  })
  return accessedRouters
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes ({ commit }, data) {
      return new Promise(resolve => {
        console.log(asyncRouterMap, 'asyncRouterMap')
        const accessedRouters = filterAsyncRouter(asyncRouterMap)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
    // GenerateRoutes ({ commit }, data) {
    //   return new Promise(resolve => {
    //     generatorDynamicRouter().then(routers => {
    //       const newRouters = asyncRouterMap
    //       newRouters[0].redirect = '/index/index'
    //       console.log(newRouters, routers,"newRouters");
    //       routers.forEach((item, index) => {
    //         item.component = RouteView
    //         if (!item.children) {
    //           return
    //         }
    //         item.children.forEach(temp => {
    //           temp.component = PageView
    //           // var _list = [...temp.children]
    //           console.log(temp)
    //           const _list = []
    //           if (!temp.children) {
    //             return
    //           }
    //           temp.children.forEach((i, iIndex) => {
    //             _list.push({
    //               path: i.path,
    //               name: i.name,
    //               hideChildrenInMenu: true,
    //               component: () => import(`@/views${i.path}.vue`),
    //               meta: { title: i.meta.title, keepAlive: true },
    //               hidden: !i.hidden
    //             })
    //             if (index == 0 && iIndex == 0) {
    //               newRouters[0].redirect = i.path
    //             }
    //           })
    //           temp.children = _list
    //           // temp.children = _list.map((i) => {
    //           //   return {
    //           //     path: i.path,
    //           //     name: i.name,
    //           //     hideChildrenInMenu: true,
    //           //     component: () => import(`@/views${i.path}.vue`),
    //           //     meta: { title: i.meta.title, keepAlive: true }
    //           //   }
    //           // })
    //         })
    //       })
    //       newRouters[0].children = [...routers]
    //       commit('SET_ROUTERS', newRouters)
    //       resolve()
    //     })
    //   })
    // }
  }
}

export default permission
